package cn.wolfcode.car.service.impl;

import cn.wolfcode.car.domain.Permission;
import cn.wolfcode.car.mapper.PermissionMapper;
import cn.wolfcode.car.qo.QueryObject;
import cn.wolfcode.car.service.IPermissionService;
import cn.wolfcode.car.util.RequiredPermission;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.method.HandlerMethod;
import org.springframework.web.servlet.mvc.method.RequestMappingInfo;
import org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerMapping;

import java.util.Collection;
import java.util.List;
import java.util.Map;

@Service
public class PermissionServiceImpl implements IPermissionService {

    @Autowired
    private PermissionMapper permissionMapper;

    @Autowired
    private RequestMappingHandlerMapping handlerMapping;


    @Override
    public void save(Permission permission) {
        permissionMapper.insert(permission);
    }

    @Override
    public void delete(Long id) {
        permissionMapper.deleteByPrimaryKey(id);
    }

    @Override
    public void update(Permission permission) {
        permissionMapper.updateByPrimaryKey(permission);
    }

    @Override
    public Permission get(Long id) {
        return permissionMapper.selectByPrimaryKey(id);
    }

    @Override
    public List<Permission> listAll() {
        return permissionMapper.selectAll();
    }

    @Override
    public PageInfo<Permission> query(QueryObject qo) {
        PageHelper.startPage(qo.getCurrentPage(),qo.getPageSize()); //对下一句sql进行自动分页
        List<Permission> permissions = permissionMapper.selectForList(qo); //里面不需要自己写limit
        return new PageInfo<Permission>(permissions);
    }

    @Override
    public void reload() {
        //查出所有已有的权限
        List<String> expressions = permissionMapper.queryByExpression();
        Map<RequestMappingInfo, HandlerMethod> handlerMethods = handlerMapping.getHandlerMethods();
        Collection<HandlerMethod> methods = handlerMethods.values();
        for (HandlerMethod method : methods) {
            RequiredPermission requiredPermission = method.getMethodAnnotation(RequiredPermission.class);
            //加载权限的核心内容
            if(requiredPermission != null){
                String expression = requiredPermission.expression();
                if(expressions.contains(expression)){
                    expressions.remove(expression);
                }else{
                    Permission permission = new Permission();
                    permission.setName(requiredPermission.name());
                    permission.setExpression(expression);
                    permissionMapper.insert(permission);
                }
            }
        }
        if(expressions != null){
            for (String expression : expressions) {
                permissionMapper.deleteByExpression(expression);
            }
        }
    }

    @Override
    public List<Permission> queryByPermission(Long employeeId) {
        return permissionMapper.queryByPermission(employeeId);
    }
}
